var regId = null;

function init() {
	_initHeader();
	$("#btn_push").click(function() {
		usePushService();
	});
	
	var array = new Array();
	
	array.push("register");
	array.push("getRegistrationId");
	array.push("getUnreadNotifications");
	array.push("useImmediateForegroundNotification");
	array.push("deleteUnreadNotification");
	array.push("deleteAllUnreadNotifications");
	array.push("unregister");
	
	draw_list(array);
}

function setOnListClickHandler(idx) {
	switch(idx) {
		case 0 :
			testPushRegister();
			
		break;
		
		case 1 :
			testGetRegistrationId();
			
		break;
		
		case 2 :
			testGetUnreadNotifications();
			
		break;
		
		case 3 :
			testUseImmediateForegroundNotification();
			
		break;
		
		case 4 :
			testDeleteUnreadNotification();
			
		break;
		
		case 5 :
			testDeleteAllUnreadNotifications();
			
		break;
		
		case 6 :
			testPushUnregister();
		break;
		
		default :

		break;
	}
}

function testPushRegister() {
	if(device.platformName == "Android") {
		var senderId = prompt("Project ID", "1090084386095");
		
		if(senderId)
			pushNotification.register(senderId);
		
	} else {
		alert("pushNotification.register is not supported on" + device.platformName + "Platform");
	}
}

function getRegistrationIdCallback(id) {
	alert("registration id : " + id);
	console.log("registration id : " + id);
}

function getUnreadNotificationsCallback(notifications) {
	if(notifications.length == 0)
		alert("No data");
	else {
		var result = "";
		for(var i = 0; i < notifications.length; i++) {
			result += "index = " + i + "\n";
			result += JSON.stringify(notifications[i]) + "\n";
		}
		
		alert(result);
	}
}
function singleSelectCallback(index) {
	if(index == 0)
		pushNotification.useImmediateForegroundNotification(true);
	else
		pushNotification.useImmediateForegroundNotification(false);
}

function testGetRegistrationId() {
	pushNotification.getRegistrationId(getRegistrationIdCallback);
}

function testGetUnreadNotifications() {
	pushNotification.getUnreadNotifications(getUnreadNotificationsCallback);
}

function testUseImmediateForegroundNotification() {
	var selection = {
		"items" : [
				"true", "false"
		],
		"defValue" : 0,
		"callback" : "singleSelectCallback",
		"title" : "useImmediateForegroundNotification",
		"x" : 100,
		"y" : 100
	};
	
	platformUIComponent.showSingleSelect(selection);
}

function testDeleteUnreadNotification() {
	var index = prompt("index", 0);
	
	if(index != null)
		pushNotification.deleteUnreadNotification(Number(index));
}

function testDeleteAllUnreadNotifications() {
	notifySuccess("deleteAllUnreadNotifications");
	
	pushNotification.deleteAllUnreadNotifications();
}

function testPushUnregister() {
	if(device.platformName == "Android") {
		pushNotification.unregister();
	} else {
		alert("pushNotification.unregister is not supported on" + device.platformName + "Platform");
	}
}

function showProgressDialogCancelCallback() {
	console.log("progress is canceled.");
}

function usePushService() {
	var option = {
		"message" : "Getting RegistrationId..",
		"cancelable" : true,
		"cancelCallback" : "showProgressDialogCancelCallback",
		"color" : "white"
	};
	
	platformUIComponent.showProgressDialog(option);
	
	if(regId == null) {
		if(device.platformName == "Android") {
			getAndroidRegId();
		} else {
			pushNotification.getRegistrationId(getRegistrationIdCallback);
		}
	} else {
		registerProvisioning();
	}
}

function getAndroidRegId() {
	pushNotification.register("1090084386095");
	setTimeout(function() {
		pushNotification.getRegistrationId(getRegistrationIdCallback);
	}, 3000);
}

function getRegistrationIdCallback(id) {
	console.log("getAndroidRegIdCallback :: id : " + id);
	
	if(id != null) {
		regId = id;
		platformUIComponent.dismissProgressDialog();
		registerProvisioning();
	} else if(device.platforName == "Android") {
		getAndroidRegId();
	}
}

function registerProvisioning() {
	var option = {
		"message" : "Register Provisioning..",
		"cancelable" : true,
		"cancelCallback" : "showProgressDialogCancelCallback",
		"color" : "white"
	};
	
	platformUIComponent.showProgressDialog(option);
	
	// 프로비저닝 등록
	var http = new Http();
	http.setRequestHeader("Content-Type", "application/json");
	
	var entity = {
		"url" : "http://203.235.211.98:7001/taurus/openapi/device/" + device.deviceId + "/" + application.appId + "?access_token=efc74d46-5b5d-4b92-9252-e2f1a3a3d685",
		"method" : "post",
		"onBody" : true,
		"content" : {
			"registrationId" : regId,
			"userId" : "alopex_runtime", // required
			"guid" : "alopex_runtime", // required
			"os" : device.platformName // required
		}
	}

	console.log("## entity: " + JSON.stringify(entity))

	http.request(entity, regSuccessCallback, regErrorCallback);
}

function push() {
	var option = {
		"message" : "Generate Push",
		"cancelable" : true,
		"cancelCallback" : "showProgressDialogCancelCallback",
		"color" : "white"
	};
	
	platformUIComponent.showProgressDialog(option);
	
	// generate push
	var http = new Http();
	http.setRequestHeader("Content-Type", "application/json");
	
	var entity = {
		"url" : "http://203.235.211.98:7001/taurus/openapi/message/single/" + application.appId + "?access_token=efc74d46-5b5d-4b92-9252-e2f1a3a3d685",
		"method" : "post",
		"onBody" : true,
		"content" : {
			"registrationId" : regId,
			"app_id" : application.appId, // required
			"guid" : "alopex_runtime",
			"send_user" : "user", // required
			"title" : "Alopex Kitchensink Push Test",
			"pageId" : "device",
			"alert" : " device 페이지로 이동합니다."
		}
	}

	console.log("## generate push entity: " + JSON.stringify(entity))

	http.request(entity, pushSuccessCallback, pushErrorCallback);
	
}

function regSuccessCallback(http) {
	platformUIComponent.dismissProgressDialog();
	console.log("regSuccessCallback");
	// alert("## register Success! " + http.response);
	push();
}

function regErrorCallback(http) {
	platformUIComponent.dismissProgressDialog();
	alert("## register Fail! " + JSON.stringify(http));
}

function pushSuccessCallback(http) {
	platformUIComponent.dismissProgressDialog();
	alert("## Success! " + http.response);
}

function pushErrorCallback(http) {
	platformUIComponent.dismissProgressDialog();
	console.log("## pushErrorCallback! " + JSON.stringify(http));
}